package org.luxor.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.luxor.commons.core.utils.BeanUtils;
import org.luxor.system.entity.SysDeptLeaderEntity;
import org.luxor.system.entity.SysUserEntity;
import org.luxor.system.mapper.SysDeptLeaderMapper;
import org.luxor.system.service.ISysDeptLeaderService;
import org.luxor.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 部门分级管理员(分管领导)
 *
 * @author Mark @date 2020-07-26 16:52:23
 */
@Service("sysDeptLeaderService")
public class SysDeptLeaderServiceImpl extends ServiceImpl<SysDeptLeaderMapper, SysDeptLeaderEntity> implements ISysDeptLeaderService {

    @Resource
    private ISysUserService sysUserService;

    @Override
    public IPage<SysDeptLeaderEntity> page(IPage<SysDeptLeaderEntity> page, Map query) {
        SysDeptLeaderEntity queryEntity = BeanUtils.toObject(SysDeptLeaderEntity.class, query);
        return super.page(page, Wrappers.query(queryEntity));
    }

    @Override
    public List<SysUserEntity> listByDeptId(Long deptId) {
        Wrapper<SysDeptLeaderEntity> queryWrapper = Wrappers.<SysDeptLeaderEntity>lambdaQuery()
                .eq(SysDeptLeaderEntity::getDeptId, deptId);
        Collection<SysDeptLeaderEntity> deptLeaders = getBaseMapper().selectList(queryWrapper);
        List<Long> deptLeaderIds = deptLeaders.stream().map(SysDeptLeaderEntity::getUserId).collect(Collectors.toList());
        List<SysUserEntity> deptLeaderUsers = (List<SysUserEntity>) sysUserService.listByIds(deptLeaderIds);
        return deptLeaderUsers;
    }
}
